package com.webcash.CNBoard.Controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.Document;
import com.webcash.CNBoard.Model.MyProject;
import com.webcash.CNBoard.Service.ProjectService;


@Controller
@RequestMapping("/project/document")
@SessionAttributes("login")
public class ProjectDocumentController {
	@Autowired
	MyProject myProject;
	@Autowired
	ProjectService projectService;
	@RequestMapping
	public String action(Model model){
		
		List<Document> docList = projectService.getDocumentListByProjectNo(myProject.getProjectNo());
		model.addAttribute("docList", docList);
		return "project/document";
	}
	@RequestMapping(value="/writeAction" , method=RequestMethod.POST)
	@ResponseBody
	public String writeAction(@RequestParam String docTitle, @RequestParam String docContents, Model model){
		
		Document doc = new Document();
		doc.setDocTitle(docTitle);
		doc.setDocContents(docContents);
		doc.setProjectNo(myProject.getProjectNo());
		doc.setDocWriter(myProject.getId());
		doc.setDocWriteDate(new Date());
		
		projectService.addDocumentByDoc(doc);
		
		return "project/document";
	}
	
	@RequestMapping("/{docNo}")
	public String pickDocument(@PathVariable int docNo, Model model){
		
		Document doc = projectService.getDocumentByDocNo(docNo);
		
		model.addAttribute("doc",doc);
		return "project/documentPick";
	}
	@RequestMapping("/delete")
	@ResponseBody
	public String deleteDocument(@RequestParam int docNo){
		projectService.removeDocumentByDocNo(docNo);
		return "success";
	}
}
